package menu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import state.Profile;
import chesskers.Driver;
import chesskers.ImagePanel;
import chesskers.UIManager;

public class MatchHistoryFrame extends MenuWindow{
	
	ImagePanel panel_mainmenu = new ImagePanel(new ImageIcon("img/box_background.png").getImage());
	JButton button_mm_quit = new JButton("Back");

	String profileFileName = "profiles.data";	

	ArrayList<Profile> profilesList = new ArrayList<Profile>();	
	
	public MatchHistoryFrame() {
		super("Chess-kers!");
		
		
		
		button_mm_quit.setBounds(150, 415, 300, 30);
		
		panel_mainmenu.add(button_mm_quit);
		MakeButtonAction(button_mm_quit, "back");		
		
		try{
			FileInputStream fIn = new FileInputStream(profileFileName);
			if(fIn.available() != 0){
				ObjectInputStream in = new ObjectInputStream(fIn);
				profilesList = (ArrayList<Profile>) in.readObject();
				in.close();
				fIn.close();
			}
			else
			{
				fIn.close();
			}
		}
		catch(IOException e){
			JOptionPane.showMessageDialog(this, "Error loading profiles");
			JOptionPane.showMessageDialog(this, e.toString());
		}
		catch(ClassNotFoundException ex){
			JOptionPane.showMessageDialog(this, "Error 2 loading profiles");
		}
		
		DefaultListModel listModel = new DefaultListModel();
		
		if (Driver.state == Driver.state.HistoryChess){
			listModel.addElement("Name        Win        Lose        Forfeight        Draw");
		}else{
			listModel.addElement("Name        Win        Lose        Forfeight");
		}
		
		for (int i = 0; i < profilesList.size(); i++){
			String info = profilesList.get(i).name+"        ";
			if (Driver.state == Driver.state.HistoryChess){
				info +=  profilesList.get(i).chessWin+"        ";
				info +=  profilesList.get(i).chessLost+"        ";
				info +=  profilesList.get(i).chessForfeit+"        ";
				info +=  profilesList.get(i).chessTie+"        ";
			}else{
				info +=  profilesList.get(i).checkerWin+"        ";
				info +=  profilesList.get(i).checkerLost+"        ";
				info +=  profilesList.get(i).checkerForfeit+"        ";
			}
			listModel.addElement(info);
		}
		
		JList list = new JList(listModel);
		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBounds(175, 80, 250, 280);
		panel_mainmenu.add(scrollPane);
		
		
		
		this.add(panel_mainmenu);
		
		setSize(600, 500);
		setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
	}

	@Override
	public void Action(String cmd) {
		// TODO Auto-generated method stub
		switch (cmd) {
		case "back":
			Driver.state = Driver.state.MainMenu;
			new UIManager().start();
		    dispose();
			break;		
		default:
			throw new UnsupportedOperationException("Invalid button code: "+cmd);
		}
	}
}